package com.meiborte.www;

import javax.swing.*;
import java.awt.*;

public class EnemyPlane extends Thread {
    public GameFrame gf;
    //子弹的坐标,大小，速度
    public int x,y;
    public int width=50;
    public int height=50;
    public int speed=2;
    public Image img=new ImageIcon("img/diji.png").getImage();

    public EnemyPlane(int x,int y,GameFrame gf){
        super();
        this.x=x;
        this.y=y;
        this.gf=gf;

    }

    public EnemyPlane(int x,int y,int width,int height,GameFrame gf){
        super();
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
        this.gf=gf;

    }
    //玛丽飞翔的逻辑：移动的逻辑都在这里。
    public void run() {
        while (true) {
            //向左走
            //碰撞到了
            if (hit()) {
                System.out.println("hit.........");
                this.speed = 0;
                this.img = new ImageIcon("img/HuDun1.png").getImage();
                try {
                    this.sleep(500);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                gf.enemys.remove(this);
                //被子弹击毁
                break;
            }
            if (this.y >= 760) {
                break;
            }
            try {
                this.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
    public boolean hit(){
        //Swing技术中,人家已经提供了！！！
        Rectangle myrect =new Rectangle(this.x,this.y,this.width,this.height);

        Rectangle rect=null;

        for (int i = 0; i < gf.bullets.size(); i++) {
            Bullet bullet=gf.bullets.get(i);
            System.out.println("test hit");
            rect =new Rectangle(bullet.x,bullet.y-1,bullet.width,bullet.height);
            //碰撞检测
            if(myrect.intersects(rect)){
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return "EnemyPlane{"+
                "x" +x+
                ",y="+y+
                ",width="+width+
                ",height="+height+
                '}';
    }
}
